<?php
// +----------------------------------------------------------------------
// | Longbing [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright Chengdu longbing Technology Co., Ltd.
// +----------------------------------------------------------------------
// | Website http://longbing.org/
// +----------------------------------------------------------------------
// | Sales manager: +86-13558882532 / +86-13330887474
// | Technical support: +86-15680635005
// | After-sale service: +86-17361005938
// +----------------------------------------------------------------------

namespace app\shortvideo\model;

use app\BaseModel;
use app\redbag\model\Record;
use app\redbag\model\staffRecord;
use think\facade\Db;

class RedbagActive extends BaseModel{
    //定义表名
    protected $name = 'longbing_card_redbag_active';

    protected $append = [

        'create_user_text'
    ];


    /**
     * @author chenniang
     * @DataTime: 2020-04-09 16:02
     * @功能说明:发布者
     */
    public function getCreateUserTextAttr($value,$data){

        if(!empty($data['create_user'])){

            $name = Db::name('longbing_card_user_info')->where(['fans_id'=>$data['create_user']])->value('name');

            if(empty($name)){

                $name = Db::name('longbing_card_user_info')->where(['id'=>$data['create_user']])->value('nickName');
            }
            return $name.'个人发布';
        }else{

            return '公司发布';
        }

    }

    /**
     * @param $dis
     * @param $page
     * @功能说明:活动列表
     * @author chenniang
     * @DataTime: 2020-03-09 18:42
     */

    public function activeList($dis,$page){
        //记录模型
        $record_model  = new Record();
        //记录模型
        $staff_record = new staffRecord();

        $sql_y = $record_model->where(['active_id' => 'a.id','status' => 1])->fetchSql()->count();
        $staff_record_sql = $staff_record->where(['active_id'=>'a.id'])->fetchSql()->count();

        $data = $this->alias('a')->where("({$staff_record_sql}) * a.num - ({$sql_y}) > 0")->where($dis)->order(['id desc'])->paginate($page)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //总数
                $count  = $staff_record->where(['active_id'=>$v['id']])->count();
                //红包总数
                $v['total_num']  = $count*$v['num'];
                //总金额
                $v['total_price']= round($v['total_num']*$v['price'],2);

                $v['price']  = round($v['price'],2);
                //已经使用的数量
                $record_dis = [
                    //活动id
                    'active_id' => $v['id'],
                    //状态
                    'status'    => 1
                ];
                $use_count   = $record_model->where($record_dis)->count();
                //剩余数量
                $v['y_num']  = $v['total_num'] - $use_count;
                //剩余金额
                $v['y_price']= round($v['y_num']*$v['price'],2);
            }
        }
        return $data;
    }

}